package top.callback.inf.beautyleaf.server.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.callback.inf.beautyleaf.server.common.LeafResult;
import top.callback.inf.beautyleaf.server.common.page.Page;
import top.callback.inf.beautyleaf.server.model.LeafTagModel;
import top.callback.inf.beautyleaf.server.service.LeafTagService;

/**
 * 标签接口
 *
 * @author Pengyu Gan
 * CreateDate 2021/4/8
 */
@Tag(name = "标签接口", description = "标签信息维护")
@RestController
@RequestMapping(value = "/api/tag")
public class LeafTagController {

    @Autowired
    private LeafTagService leafTagService;

    @Operation(summary = "新增标签", parameters = {
            @Parameter(name = "tagModel", description = "标签信息模型", required = true)
    })
    @PostMapping(value = "/add")
    public LeafResult<Boolean> addTag(@RequestBody LeafTagModel tagModel) {
        boolean success = leafTagService.addTag(tagModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "编辑标签", parameters = {
            @Parameter(name = "tagModel", description = "标签信息模型", required = true)
    })
    @PostMapping(value = "/edit")
    public LeafResult<Boolean> editTag(@RequestBody LeafTagModel tagModel) {
        boolean success = leafTagService.editTag(tagModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "删除标签", parameters = {
            @Parameter(name = "tagId", description = "标签 ID", required = true)
    })
    @GetMapping(value = "/remove/{tagId}")
    public LeafResult<Boolean> removeTag(@PathVariable("tagId") long tagId) {
        boolean success = leafTagService.removeTag(tagId);
        return LeafResult.success(success);
    }

    @Operation(summary = "查询指定标签", parameters = {
            @Parameter(name = "tagId", description = "标签 ID", required = true)
    })
    @GetMapping(value = "/get/{tagId}")
    public LeafResult<LeafTagModel> findById(@PathVariable("tagId") long tagId) {
        LeafTagModel tagModel = leafTagService.findById(tagId);
        return LeafResult.success(tagModel);
    }

    @Operation(summary = "查询所有标签", parameters = {
            @Parameter(name = "pageNo", description = "页码", required = true),
            @Parameter(name = "pageSize", description = "数据量", required = true)
    })
    @GetMapping(value = "/list/{pageNo}/{pageSize}")
    public LeafResult<Page<LeafTagModel>> findAll(@PathVariable("pageNo") long pageNo, @PathVariable("pageSize") long pageSize) {
        Page<LeafTagModel> page = leafTagService.findAll(pageNo, pageSize);
        return LeafResult.success(page);
    }

    @Operation(summary = "启用/禁用标签", parameters = {
            @Parameter(name = "tagId", description = "标签 ID", required = true)
    })
    @GetMapping(value = "/switch/{tagId}")
    public LeafResult<Boolean> switchTag(@PathVariable("tagId") long tagId) {
        boolean success = leafTagService.switchTag(tagId);
        return LeafResult.success(success);
    }
}